<?php
/** 
 * 
 *
 * @package api
 * @subpackage db
 * @author Richard Fullmer <php-api@byu.edu>
 * @version 1.0
 */
/**
 *
 */
require_once( "lang/Number.php" );
require_once( "lang/NumberFormatException.php" );
require_once( "lang/Comparable.php" );

/**
 * Class Integer
 *
 * The Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field 
 * whose type is int.
 * 
 * In addition, this class provides several methods for converting an int to a String and a String to an int, as well as other 
 * constants and methods useful when dealing with an int. 
 *
 * @package api
 * @subpackage db
 */
class Integer extends Number implements Comparable {

	// ======================================== Private Members ====================================================

	private $value;
	
	// ======================================== Public Methods =====================================================
	
	/**
	 * Constructs a newly allocated Integer object that represents the specified int value.
	 *
	 * @throws NumberFormatException if the $value cannot be converted into an int.
	 */
	public function __construct( $value ) {
		if ( is_int( $value ) )
			$this->value = $value;
		else
			throw new NumberFormatException();
	}
	
	/**
	 * Compares this object to the specified object. The result is true if and only if the argument is not null and is 
	 * an Integer object that contains the same int value as this object.
	 * 
	 * @param Object $o the object to compare with
	 * @returns boolean true if the objects are the same, false otherwise.
	 */
	public function equals( Object $o ) {
		if ($o instanceof Integer) {
			return $this->intValue() == $o->intValue();
		}
		return false;
	}
	
	/**
	 * Compares two another object with this object.
	 *
	 * @param Object $o the Object to be compared
	 * @returns int the value 0 if this Integer is equal to the argument Integer; a value less than 0 if this Integer is 
	 * 		numerically less than the argument Integer; and a value greater than 0 if this Integer is numerically greater than 
	 * 		the argument Integer (signed comparison).
	 * @throws ClassCastException - if the object $c cannot be compared with this Integer instance.
	 */
	public function compareTo( Object $o ) {
		if (!$o instanceof Integer)
			throw new ClassCastException();
		
		return ($this->intValue() < $o->intValue() ? -1 : ($this->value == $o->intValue() ? 0 : 1 ));
	}
	
	/**
	 * Returns a hash code for this Integer.
	 *
	 * @returns int a hash code value for this object, equal to the primitive int value represented by this Integer object.
	 */
	public function hashCode() {
		return $this->intValue();
	}
	
	
	/**
	 * Returns the value of this Float object as an int (by casting to type int).
	 *
	 * @returns int the float value represented by this object converted to type int.
	 */
	public function intValue() {
		return (int) $this->value;
	}
	
	/**
	 * Returns the value of this Float object as a boolean (by casting to type boolean).
	 *
	 * @returns boolean the float value represented by this object converted to type boolean.
	 */
	public function booleanValue() {
		return (boolean) $this->value;
	}
	
	/**
	 * Returns the value of this Float object as an float.
	 *
	 * @returns float the float value represented by this object.
	 */
	public function floatValue() {
		return (float) $this->value;
	}
	
	/**
	 * Returns the value of this Float object as an string (by casting to type string).
	 *
	 * @returns string the float value represented by this object converted to type string.
	 */
	public function stringValue() {
		return (string) $this->value;
	}
}

?>